import 'package:flutter/material.dart';
// 1. 导入依赖包
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

// 2. 添加 part 文件
part 'main.g.dart';

void main() {
  runApp(
    // 3. 使用 ProviderScope 包裹根组件
    ProviderScope(
      child: MyApp(),
    ),
  );
}

// 4. 使用 @riverpod
// 4.1 等同于 final helloProvider = Provider<String>((ref) => Hello world!');
@riverpod
String helloWorld(Ref ref) {
  // 可以手动指定接受参数
  return 'Hello world!';
}

// 4.2 等同于 final counterProvider = StateProvider<int>((ref) => 0);
@riverpod
class Counter extends _$Counter {
  @override
  int build() {
    return 0;
  }

  void increment() {
    state++;
  }
}

// 4.3 等同于 final urlProvider = FutureProvider<int>((ref) => 'https://example.com');
@riverpod
Future<String> url(Ref ref) async {
  await Future.delayed(const Duration(seconds: 3));
  return 'https://example.com';
}

// 4.4 等同于 final urlProvider = StreamProvider<int>((ref) => 'https://example.com');
@riverpod
Stream<String> streamUrl(Ref ref) async* {
  yield 'https://example.com';
}

// 4.5 开启 keepAlive
@Riverpod(keepAlive: false)
int keepAlive(Ref ref) => 0;

class MyApp extends ConsumerWidget {
  @override
  Widget build(BuildContext context, WidgetRef ref) {
    // 消费 Provider
    final String value = ref.watch(helloWorldProvider) +
        ref
            .watch(counterProvider)
            .toString(); // 使用 ref.watch 监听 counterProvider 的值
    final AsyncValue<String> urlValue = ref.watch(urlProvider);
    final AsyncValue<String> streamUrlValue = ref.watch(streamUrlProvider);

    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('Example')),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text(value),
              ElevatedButton(
                // 使用 ref.read 获取 counterProvider 并调用其方法更新 counterProvider 的值
                onPressed: () => ref.read(counterProvider.notifier).increment(),
                child: const Text('Update'),
              ),
              // 消费 FutureProvider
              urlValue.when(
                data: (data) => Text('URL: $data'),
                error: (error, stackTrace) => Text('URL Error: $error'),
                loading: () => const CircularProgressIndicator(),
              ),
              // 等同于 urlValue.when(
              switch (urlValue) {
                AsyncData(:final value) => Text(value),
                AsyncError() => const Text('Error'),
                _ => const CircularProgressIndicator(),
              },
              // 消费 StreamProvider
              streamUrlValue.when(
                data: (data) => Text('Stream URL: $data'),
                error: (error, stackTrace) => Text('Stream URL Error: $error'),
                loading: () => const CircularProgressIndicator(),
              ),
              // 等同于 streamUrlValue.when(
              switch (streamUrlValue) {
                AsyncData(:final value) => Text(value),
                AsyncError() => const Text('Error'),
                _ => const CircularProgressIndicator(),
              },
            ],
          ),
        ),
      ),
    );
  }
}
